package org.fuxia.im.core.service.impl;

import javax.annotation.Resource;

import org.fuxia.im.core.entries.ECPacket;
import org.fuxia.im.core.entries.ECResponse;
import org.fuxia.im.db.dao.UserDaoImpl;
import org.fuxia.im.entities.User;
import org.springframework.stereotype.Service;

@Service(value="MODIFY_PASS")
public class ModifyPasswordService  extends AbstractService{

	@Resource
	private UserDaoImpl userDao ;
	
	public void service(ECPacket packet) {
		String orignPass = packet.getParameter("o_pass") ;
		String newPass = packet.getParameter("new_pass") ;
		String from = packet.getFrom() ;
		ECResponse resp = new ECResponse() ;
		User u = userDao.getByPK(from) ;
		if(u != null) {
			if(u.getPassword()!=null && orignPass.equals(u.getPassword())) {
				u.setPassword(newPass) ;
				try{
					userDao.merge(u);
					resp.setStatusCode(200);
					resp.setResponseText("密码修改成功") ;
				}catch (Exception e) {
					resp.setResponseText("修改密码失败!") ;
					resp.setStatusCode(400) ;
				}
				
			}else {
				resp.setStatusCode(400) ;
				resp.setResponseText("初始密码输入错误") ;
			}
		}else {
			resp.setStatusCode(400);
			resp.setResponseText("用户不存在") ;
		}
		writeReponse(packet, resp);
	}
}
